<template>
  <div class="dashboard-list-container">
    <div class="content-area">
      <div class="layout-container">
        <!-- 左侧分组列表 -->
        <div class="left-sidebar">
          <div class="group-title">看板分组</div>
          <div class="group-list">
            <div 
              v-for="group in dashboardGroups" 
              :key="group.id" 
              class="group-item" 
              :class="{ active: currentGroupId === group.id }"
              @click="selectGroup(group.id)"
            >
              {{ group.name }} <span class="count">({{ group.count }})</span>
            </div>
            <div class="add-group">
              <a-button type="text" block class="add-group-btn" @click="showCreateGroupModal = true">
                <plus-outlined />
                新建分组
              </a-button>
            </div>
          </div>
        </div>

        <!-- 右侧看板列表 -->
        <div class="main-content">  
          <div v-if="loading" class="loading-container">
            <a-spin size="large" />
          </div>        
          <div v-else class="board-container">
            <div class="boards-row">
              <!-- 新建看板卡片 -->
              <div class="board-card new-board" @click="createNewDashboard">
                <div class="add-icon">
                  <plus-outlined />
                </div>
                <div class="board-title">新建看板</div>
              </div>
              
              <!-- 示例看板卡片 -->
              <div 
                v-for="board in dashboards" 
                :key="board.id" 
                class="board-card"
                @click="openDashboard(board.id)"
              >
                <div class="board-title">{{ board.title }}</div>
                <div class="board-preview">
                  <div class="preview-images">
                    <img v-if="board.thumbnail" :src="board.thumbnail" alt="看板预览" />
                    <div v-else class="preview-placeholder">
                      <div class="chart-icon"></div>
                      <div class="chart-icon"></div>
                      <div class="chart-icon"></div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 新建分组对话框 -->
    <a-modal
      v-model:visible="showCreateGroupModal"
      title="新建分组"
      @ok="createGroup"
      :confirmLoading="creatingGroup"
    >
      <a-form :model="groupForm">
        <a-form-item label="分组名称" name="name" :rules="[{ required: true, message: '请输入分组名称' }]">
          <a-input v-model:value="groupForm.name" placeholder="请输入分组名称" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { PlusOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import { getDashboardGroups, getDashboardList, createDashboard, createDashboardGroup } from '../api/dashboard';
import type { DashboardGroup, Dashboard } from '../api/dashboard';

const router = useRouter();
const loading = ref(true);
const dashboardGroups = ref<DashboardGroup[]>([]);
const dashboards = ref<Dashboard[]>([]);
const currentGroupId = ref<string>('');

// 新建分组相关
const showCreateGroupModal = ref(false);
const creatingGroup = ref(false);
const groupForm = reactive({
  name: ''
});

// 获取分组列表
const fetchGroups = async () => {
  try {
    const res = await getDashboardGroups();
    if (res.code === 200) {
      dashboardGroups.value = res.data;
      if (dashboardGroups.value.length > 0) {
        currentGroupId.value = dashboardGroups.value[0].id;
      }
    } else {
      message.error('获取分组失败');
    }
  } catch (error) {
    console.error('获取分组错误', error);
    message.error('获取分组出错');
  }
};

// 获取看板列表
const fetchDashboards = async () => {
  loading.value = true;
  try {
    const res = await getDashboardList({ groupId: currentGroupId.value });
    if (res.code === 200) {
      dashboards.value = res.data;
    } else {
      message.error('获取看板列表失败');
    }
  } catch (error) {
    console.error('获取看板列表错误', error);
    message.error('获取看板列表出错');
  } finally {
    loading.value = false;
  }
};

// 选择分组
const selectGroup = (groupId: string) => {
  currentGroupId.value = groupId;
  fetchDashboards();
};

// 创建分组
const createGroup = async () => {
  if (!groupForm.name.trim()) {
    message.error('请输入分组名称');
    return;
  }

  creatingGroup.value = true;
  try {
    const res = await createDashboardGroup({ name: groupForm.name.trim() });
    
    if (res.code === 200) {
      message.success('创建分组成功');
      // 将新分组添加到列表
      dashboardGroups.value.push(res.data);
      // 选择新创建的分组
      currentGroupId.value = res.data.id;
      // 清空表单并关闭对话框
      groupForm.name = '';
      showCreateGroupModal.value = false;
      // 加载新分组的看板
      fetchDashboards();
    } else {
      message.error(res.message || '创建分组失败');
    }
  } catch (error) {
    console.error('创建分组错误', error);
    message.error('创建分组出错');
  } finally {
    creatingGroup.value = false;
  }
};

// 打开看板
const openDashboard = (id: string) => {
  router.push(`/dashboard/${id}`);
};

// 创建新看板
const createNewDashboard = async () => {
  try {
    const res = await createDashboard({
      title: `新建看板 ${Date.now()}`,
      groupId: currentGroupId.value,
      description: '新建的空白看板'
    });
    
    if (res.code === 200) {
      message.success('创建看板成功');
      // 跳转到新看板页面
      router.push(`/dashboard/${res.data.id}`);
    } else {
      message.error(res.message || '创建看板失败');
    }
  } catch (error) {
    console.error('创建看板错误', error);
    message.error('创建看板出错');
  }
};

onMounted(async () => {
  await fetchGroups();
  await fetchDashboards();
});
</script>

<style scoped>
.dashboard-list-container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding: 0;
  margin: 0;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.header-left {
  display: flex;
  align-items: center;
}

.content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.layout-container {
  display: flex;
  height: 100%;
}

.left-sidebar {
  width: 200px;
  background-color: #fff;
  border-right: 1px solid #eee;
  box-sizing: border-box;
}

.group-title {
  font-size: 14px;
  font-weight: bold;
  padding: 12px 16px;
  border-bottom: 1px solid #eee;
}

.group-list {
  padding: 12px 0;
}

.group-item {
  padding: 8px 16px;
  cursor: pointer;
  transition: background-color 0.3s;
  display: flex;
  justify-content: space-between;
}

.group-item:hover {
  background-color: #f5f5f5;
}

.group-item.active {
  background-color: #e6ffed;
  color: #42b983;
}

.count {
  color: #999;
  font-size: 12px;
}

.add-group {
  padding: 8px 16px;
  margin-top: 8px;
}

.add-group-btn {
  text-align: left;
  color: #999;
}

.main-content {
  flex: 1;
  padding: 0 16px;
  overflow: auto;
}

.tab-header {
  display: flex;
  border-bottom: 1px solid #eee;
  margin-bottom: 16px;
}

.tab-item {
  padding: 12px 16px;
  cursor: pointer;
  position: relative;
  color: #666;
}

.tab-item.active {
  color: #1890ff;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #1890ff;
}

.board-container {
  padding: 0;
}

.boards-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.board-card {
  width: 240px;
  height: 160px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  cursor: pointer;
  display: flex;
  flex-direction: column;
}

.new-board {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px dashed #ccc;
  background: #fafafa;
}

.add-icon {
  font-size: 24px;
  color: #42b983;
  margin-bottom: 8px;
}

.board-title {
  padding: 12px;
  font-size: 14px;
  color: #333;
  border-bottom: 1px solid #f0f0f0;
}

.board-preview {
  flex: 1;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-images {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.preview-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  opacity: 0.5;
}

.chart-icon {
  width: 48%;
  height: 48%;
  background: #f5f5f5;
  border-radius: 2px;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  width: 100%;
}
</style> 